#include "editor.h"
#include "board.h"
#include "events.h"
#include "gui.h"
#include "util.h"
#include <stdlib.h>

static Board* editedBoard = NULL;

extern Gui gui;

/* Utwórz świeże okienko, wyzeruj wszystkie wartości... */
void editor_clear_board() {

	if(editedBoard != NULL)
		board_cleanup(editedBoard);

	editedBoard = NULL;		
	gtk_entry_set_text (gui.editorEntry, "0");
	gtk_spin_button_set_value (gui.editorSpinButton, 5);
	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(gui.editorCircleBegins), TRUE);

}

/* Wyczyść dane */
void editor_cleanup() {

	if(editedBoard != NULL) {
		board_cleanup(editedBoard);
		editedBoard = NULL;
	}
	
}

/* odrysowanie drawingArea edytora */
gboolean editor_drawingarea_paint(GtkWidget *widget, GdkEventExpose *event, gpointer data) {

	if(editedBoard != NULL)
		gtk_widget_show_all(GTK_WIDGET(gui.editorFixed));
	else
		gtk_widget_hide_all(GTK_WIDGET(gui.editorFixed));

	return draw_board_on_drawing_area(widget, editedBoard);
	
}

gboolean editor_board_clicked(GtkWidget *widget, GdkEvent *event, gpointer user_data) {

	Vector boardCoord;
	Vector clickedCoord;
	Vector windowSize;
	GdkEventButton* ev;
	GtkAllocation alloc;
	symbol s;

	/* pusta plansza */
	if(editedBoard == NULL)
		return FALSE;

	/* pobieramy współrzędne klikniętego pola */
	
	ev = (GdkEventButton*)event;

	clickedCoord.x = ev->x;
	clickedCoord.y = ev->y;

	gtk_widget_get_allocation (widget, &alloc);
	windowSize.x = alloc.width;
	windowSize.y = alloc.height;

	boardCoord = translate_point_to_coord(board_size(editedBoard),
	                                        clickedCoord, windowSize);

	/* klik poza obszarem */
	if(boardCoord.x == -1 || boardCoord.y == -1)
		return FALSE;

	/* PUSTE -> KÓŁKO -> KRZYŻYK za każdym kliknięciem */
	s = board_get(editedBoard, boardCoord.x, boardCoord.y);

	if(s == EMPTY)
		board_set(editedBoard, boardCoord.x, boardCoord.y, CIRCLE);
	else if(s == CIRCLE)
		board_set(editedBoard, boardCoord.x, boardCoord.y, CROSS);
	else
		board_set(editedBoard, boardCoord.x, boardCoord.y, EMPTY);

	/* odśwież */
	gtk_widget_queue_draw (widget);

	return FALSE;
	
}

/* nowa plansza */
void editor_new_clicked(GtkToolButton* button) {

	int ret;
	int boardSize;

	/* pobieramy od użytkownika rozmiar planszy */
	gtk_spin_button_set_value(gui.editorBoardSize, 7.0);
	ret = gtk_dialog_run(gui.editorBoardSizeDialog);
	gtk_widget_hide(GTK_WIDGET(gui.editorBoardSizeDialog));

	if(ret != GTK_RESPONSE_OK)
		return;

	boardSize = (int)gtk_spin_button_get_value(gui.editorBoardSize);

	/* pusta plansza */
	if(editedBoard != NULL)
		board_cleanup(editedBoard);

	board_init(&editedBoard, boardSize, 5, 0.0, 0.0, FALSE, FALSE);

	gtk_entry_set_text (gui.editorEntry, "0");
	gtk_spin_button_set_value (gui.editorSpinButton, 5);
	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(gui.editorCircleBegins), TRUE);

	gtk_widget_queue_draw(GTK_WIDGET(gui.editorDrawingArea));

}

/* otwórz planszę */
void editor_open_clicked(GtkToolButton* button) {

	char buffer[50];

	int result = read_from_file(&editedBoard, GTK_WIDGET(gui.editor));

	if(result == 0) {

		/* ustaw wartości */
		sprintf(buffer, "%d", (int)board_get_cross_bidding_points(editedBoard));
		gtk_entry_set_text(gui.editorEntry, buffer);
		gtk_spin_button_set_value(gui.editorSpinButton, board_get_penalty(editedBoard));

		if(board_get_current_player_symbol(editedBoard) == CIRCLE)
			gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(gui.editorCircleBegins), TRUE);
		else
			gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(gui.editorCrossBegins), TRUE);

	}

	gtk_widget_queue_draw(GTK_WIDGET(gui.editorDrawingArea));

}

/* zapisz planszę */
void editor_save_clicked(GtkToolButton* button) {

	int biddingPoints;

	board_set_penalty(editedBoard, (int)gtk_spin_button_get_value(gui.editorSpinButton));

	sscanf(gtk_entry_get_text (gui.editorEntry), "%d", &biddingPoints);
	
	board_set_cross_bidding_points(editedBoard, biddingPoints+0.5f);

	if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(gui.editorCircleBegins)))
		board_set_current_player_symbol(editedBoard, CIRCLE);
	else
		board_set_current_player_symbol(editedBoard, CROSS);

	save_to_file(editedBoard, GTK_WIDGET(gui.editor));

}

/* otwieramy okno */
void editor_show(GtkWidget* window) {

	if(editedBoard != NULL)
		board_cleanup(editedBoard);

	board_init(&editedBoard, 7, 5, 0.0f, 0.0f, FALSE, FALSE);

}

/* zamykamy okno */
void editor_hide(GtkWidget* window) {

	if(editedBoard != NULL)
		board_cleanup(editedBoard);

	editedBoard = NULL;
	
}